package com.jjlin.leetcode;

public class Solution10 {

    //正则表达式匹配
    public boolean isMatch(String s, String p) {
        int m = s.length();
        int n = p.length();
        boolean[][] dp = new boolean[m + 1][n + 1];
        char[] s1 = s.toCharArray();
        char[] p1 = p.toCharArray();
        for(int i = 0; i <= m; i++){
            for(int j = 0; j <= n; j++){
                if(j == 0){
                    dp[i][j] = i == 0;
                    continue;
                }
                if(p1[j - 1] != '*'){
                    if(i >= 1 && (p1[j - 1] == s1[i - 1] || p1[j - 1] == '.'))
                        dp[i][j] = dp[i - 1][j - 1];
                }else{
                    if(j >= 2)
                        dp[i][j] |= dp[i][j - 2];
                    if(i >= 1 && j >= 2 && (s1[i - 1] == p1[j - 2] || p1[j - 2] == '.'))
                        dp[i][j] |= dp[i - 1][j];
                }
            }
        }
        return dp[m][n];
    }
}
